function [OF, psi,rhoj,sj, sjH, sjNH, s0j,s0jH, s0jNH, etaEj,etaDj,vdj, piDprimej, dealer_ids_out, uB, uE, piB, piE, YB, YE, Ecost, uE2, psi_opt, rhoj_opt, W, Wxij, Wvdj, WxijH, W2, WwH, WxijwH, WvdjwH, WxijHwH, W2wH] = model_prediction_avg_H(q, Yt, params, side, SQ, CF, access,vdjSQ, idxj)
 
global Imax Dmax  xij_for_investor eta homedealer 

%sort Yt on id and then dealer 
Yt = sortrows(Yt, [1,16,26]); 

if CF==0 && SQ==0
    estimate=1;
else
    estimate=0;
end

%index for dealers
dealer_ids              = Yt(idxj,16);
idxd                        = find(ismember(Dmax,dealer_ids));
dealer_ids_out      =  NaN*ones(Imax,1);
dealer_ids_out(idxd)=dealer_ids;

if CF==0 || CF<0
    idxjH        = find(Yt(:,26)==1 & Yt(:,25)==1 );
    idxjB        = find(Yt(:,26)==1 & Yt(:,25)==0 );    

else
    idxjH        = find(Yt(:,26)==1 & Yt(:,25)==1 & Yt(:,3)==0);
    idxjB        = find(Yt(:,26)==1 & Yt(:,25)==0 & Yt(:,3)==0); 
end

dealer_idsH  = Yt(idxjH,16);
idxdH        = find(ismember(Dmax,dealer_idsH));
    
dealer_idsB  = Yt(idxjB,16);
idxdB        = find(ismember(Dmax,dealer_idsB));


%fill in quotes
qfull = NaN*ones(Imax,1);
if (CF>=8 && CF<=15) || CF<0 || CF==17 || CF==18
    qfull = q;
else 
    qfull(idxd)=q;
end 

%label params
sigma   = Yt(1,15);
munu    = params(1);
sigmanu = params(2);
c       = params(3);

Jt = length(idxj);

xijH        = nanmedian(Yt(idxj,24))*ones(Imax,1); 
xijH(idxd) = Yt(idxj,24); 

xijB        = nanmedian(Yt(idxj,23))*ones(Imax,1); 
xijB(idxd) = Yt(idxj,23); 
    
    %TO RUN THE EXTENDED MODEL, SETTING HOMEDEALER BENEFIT TO 0, USE THE BELOW
    %if retailer==1
    %xijH        = nanmedian(Yt(idxj,23))*ones(Imax,1); 
    %xijH(idxd) = Yt(idxj,23); 

    %xijB        = nanmedian(Yt(idxj,23))*ones(Imax,1); 
    %xijB(idxd) = Yt(idxj,23); 
    %end
    
%set the quality of the dealer who is inactive to NaN (if Jt<Imax)
xijB(setdiff(1:Imax, idxd))=NaN;
xijH(setdiff(1:Imax, idxd))=NaN;
    
%create a xij matrix with xij's from each dealer j's percpective
xij = NaN*ones(Imax,Imax); 
    %Note: matrix with the xij's from dealer j's perspective
                   %column j are the xij's from dealer j's perspective,
                   %with (j,j) as her home dealer quality and all other
                   %entries as non-homedealer qualities
for j=1:Imax
    xij(:,j) = xijB;
	xij(j,j) = xijH(j); %quality with base qualities of all dealers buy j and homedealer quality for j
end
    
%set quality of the dealer who is inactive to NaN (if Jt<Imax)
xij(:, setdiff(1:Imax, idxd))=NaN;

if xij_for_investor==1
    xid     = 0;               %set to 0 if psi does not include xid
    
elseif  xij_for_investor==0 
    if eta==0
        xid = Yt(idxj,24) -Yt(idxj,23);
        
        %TO RUN THE EXTENDED MODEL, SETTING HOMEDEALER BENEFIT TO 0, USE THE BELOW
        %xid = (Yt(idxj,24) -Yt(idxj,23))*0; 
    end 
end 

%distribution functions
f = @(x) normpdf(x,munu,sigmanu);
F = @(x) normcdf(x,munu,sigmanu);
   

%initialize so that all outputes have the same number of dealers
psi        =  NaN*ones(Imax,1);
rhoj       =  NaN*ones(Imax,1);
psi_opt    =  NaN*ones(Imax,1);
rhoj_opt   =  NaN*ones(Imax,1);
s0j        =  NaN*ones(Imax,1);
sj         =  NaN*ones(Imax,1);
sjH        =  zeros(Imax,1);
sjNH       =  zeros(Imax,Imax,1);
s0jH        =  zeros(Imax,1);
s0jNH       =  zeros(Imax,Imax,1);
etaEj      =  NaN*ones(Imax,1);
etaDj      =  NaN*ones(Imax,1);
piDprimej  =  NaN*ones(Imax,1);
vdj        =  NaN*ones(Imax,1);
uB         =  NaN*ones(Imax,1);
uE         =  NaN*ones(Imax,1);
uE2        =  NaN*ones(Imax,1);
piB        =  NaN*ones(Imax,1);
piE        =  NaN*ones(Imax,1);
YB         =  NaN*ones(Imax,1);
YE         =  NaN*ones(Imax,1);
Ecost      =  NaN*ones(Imax,1);

%use mean theta per dealer (to have a single theta per day)
theta= ones(Imax,1)*mean(Yt(idxj, 5));


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%BUYERS WITH ACCESS 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if access==1 && side==1
    
    %PSI CUTOFF
    for j=1:Jt
        if xij_for_investor==1
            xidj=0;
        else 
            xidj=xid(j);
        end
              
        psi(idxd(j),:)     =  sigma*log(sum(exp(1/sigma*(xij(idxd,idxd(j))+qfull(idxd)))))-xidj +c- theta(idxd(j));
        psi_opt(idxd(j),:) =  max(xij(idxd,idxd(j))+qfull(idxd)) +c- theta(idxd(j));
           
        %When we shift everyone on the platform
        if CF==16 || CF==17 || CF==18  
            psi(idxd(j),:)= Inf;
            psi_opt(idxd(j),:) = Inf;
        end 
           
    end
   
    %MARKET SHARES
    %(a) OTC makret
    rhoj(idxd,:)     = 1-F(psi(idxd,:));
    rhoj_opt(idxd,:) = 1-F(psi_opt(idxd,:));
    
	%(b) platform
    s0jNH=NaN*ones(Imax,Imax);
    s0kH =NaN*ones(Imax,Imax);
    sjNH =NaN*ones(Imax,Imax);
    s0jNHprime =NaN*ones(Imax,Imax);
    h               = NaN*ones(Imax,1);
    s0jH            = NaN*ones(Imax,1);
    s0jHprime        = NaN*ones(Imax,1);
    sjprime         = NaN*ones(Imax,1);
    etaEj           = NaN*ones(Imax,1);
    etaDj           = NaN*ones(Imax,1);  
    
     
    for j=1:Jt
        idxdother       = setdiff(1:Imax, idxd(j));
       
        %conditional platform market share and conditional on which dealer is the homedealer
        s0jH(idxd(j),:)  = exp(1/sigma*(xij(idxd(j),idxd(j)) + qfull(idxd(j))))/(sum(exp(1/sigma*(xij(idxd,idxd(j))+qfull(idxd)))));
          
        for k=1:Jt-1
            s0jNH(idxd(j),idxdother(k)) = exp(1/sigma*(xijB(idxd(j)) + qfull(idxd(j))))/(sum(exp(1/sigma*(xij(idxd,idxdother(k))+qfull(idxd)))));
            s0kH(idxd(j),idxdother(k))  = exp(1/sigma*(xijB(idxdother(k)) + qfull(idxdother(k))))/(sum(exp(1/sigma*(xij(idxd,idxd(j))+qfull(idxd))))); %investors with home dealer j who buy from dealer k
        end

        %conditional platform market share, unconditional on who is the homedealer 
        h(idxd) = (1-rhoj(idxd,:))./ nansum(1-rhoj(idxd,:));
        s0j(idxd(j),:)= h(idxd(j)).*s0jH(idxd(j),:) + nansum(h(idxdother)'.*s0jNH(idxd(j),idxdother));
        
        %unconditional platform market share
        sjH(idxd(j),:)          = s0jH(idxd(j),:)*F(psi(idxd(j))); %of home investors
        sjNH(idxd(j),idxdother) = (s0jNH(idxd(j),idxdother)'.*F(psi(idxdother)))'; %of home investors from dealer another dealer (in idxother)
        sj(idxd(j),:)           = sjH(idxd(j),:) + nansum(sjNH(idxd(j),idxdother)); %total              
    end
    
  
    if CF==8 || CF==9 || CF==10 || CF==11 || CF==-2 || CF==18
        best_dealer= NaN*ones(Imax,1);
        s0jNH=NaN*ones(Imax,Imax);
        sjNH =NaN*ones(Imax,Imax);
        s0jH= NaN*ones(Imax,1);
    
        for j=1:Jt
             
            best_dealer(idxd(j),:)=find(max(xij(idxd,idxd(j))+qfull(idxd)) ==xij(idxd,idxd(j))+qfull(idxd));
            %a) for investors of home dealer idxd(j), idxd(j) is the best dealer
            if idxd(j) == best_dealer(idxd(j))
                s0jH(idxd(j),:) = 1;
            else 
                s0jH(idxd(j),:) = 0;
            end 
            sjH(idxd(j),:)       = s0jH(idxd(j),:)*F(psi(idxd(j))) ;%of home investors
        
            %b) for investors of home dealer idxd(j), another dealer is the best so s0jNH(other dealer, home dealer j) =1
            idxdother       = setdiff(1:Imax, idxd(j));
            for k=1:Jt-1
                if best_dealer(idxd(j))==idxdother(k) 
                    s0jNH(idxdother(k),idxd(j)) = 1;
                else 
                    s0jNH(idxdother(k),idxd(j)) = 0;
                end
            end
        
        end 
        for j=1:Jt
            idxdother       = setdiff(1:Imax, idxd(j));
            sjNH(idxd(j),idxdother) = (s0jNH(idxd(j),idxdother)'.*F(psi(idxdother)))'; %of home investors from dealer another dealer (in idxother)
            sj(idxd(j),:)           = sjH(idxd(j),:) + nansum(sjNH(idxd(j),idxdother));   
        end
    end
    
   
    for j=1:Jt
        
        s0jHprime(idxd(j),:)    = 1/sigma * s0jH(idxd(j),:) * nansum(s0jNH( :, idxd(j)));
        
        idxdother = setdiff(1:Imax, idxd(j));
        for k=1:length(idxdother)
            idxotherkj=setdiff(idxdother, idxdother(k));
            s0jNHprime(idxd(j),idxdother(k))   =  1/sigma *s0jNH(idxd(j), idxdother(k))*(s0jH(idxdother(k),:)+nansum(s0jNH(idxdother(k), idxotherkj)));
        end
    end 
    
          
    for j=1:Jt
        idxdother = setdiff(1:Imax, idxd(j));
        
        sjprime(idxd(j),:) = s0jHprime(idxd(j),:).*F(psi(idxd(j))) + nansum(s0jNHprime(idxd(j),idxdother)'.* F(psi(idxdother))) ...
                                 + s0jH(idxd(j),:).^2*f(psi(idxd(j)))  + nansum(s0jNH(idxd(j),idxdother).^2'.* f(psi(idxdother)));
    end       
    etaEj(idxd,:) = sjprime(idxd)./sj(idxd) .*qfull(idxd);
    etaDj(idxd,:) = -f(psi(idxd)).*s0jH(idxd)/(1-F(psi(idxd)))*qfull(idxd);
       
     
    %VALUE OF DEALER
    if homedealer==0 && xij_for_investor==1     %benchmark in benchark model
        xijnow = xij;
    elseif homedealer==0 && xij_for_investor==0  
    	xijnow = 0;
    elseif homedealer==1 && xij_for_investor==0 %benchmark with homedealer model
    	xijnow = xijB(idxd);
    elseif homedealer==1 && xij_for_investor==1 
        xijnow = xijH(idxd);
    end 
        
    if estimate==1
        %back out value
        a0(idxd,:)      = etaDj(idxd,:)./etaEj(idxd,:) .*rhoj(idxd,:)./sj(idxd,:);
        vdj(idxd,:) 	= (qfull(idxd) .*(1+1./etaEj(idxd,:))+ (psi(idxd,:) +theta(idxd)- xijnow ).*a0(idxd,:))./(1+a0(idxd,:));

    elseif estimate==0
        %use the estimated value of the dealer    
        vdj(idxd,:)     =vdjSQ(:,idxd)';
    end 
    
    
    %Shrinking dealer's value towards theta
    distance = vdj-theta;
    vdj2     = theta+(1-0.16)*distance;
      

    %OBJECTIVE FUNCTION FOR QUOTES
     a0(idxd,:)      = etaDj(idxd,:)./etaEj(idxd,:) .*rhoj(idxd,:)./sj(idxd,:);      
     OF0=sum((vdj(idxd,:) 	- (qfull(idxd) .*(1+1./etaEj(idxd,:))+ (psi(idxd,:) +theta(idxd)- xijnow ).*a0(idxd,:))./(1+a0(idxd,:))).^2);
     
     if CF==16 || CF== 17 || CF==18 
         OF0=sum((vdj(idxd,:) 	- (qfull(idxd) .*(1+1./etaEj(idxd,:)))).^2);
     end 
      
    if isnan(OF0)==0 
        %OF = sum((vdj(idxd,:) - qfull(idxd).*(1+1./etaEj(idxd,:).*(1-piDprimej(idxd,:)./sj(idxd,:)))).^2);
        OF=OF0;
    else 
        OF = 10^100;
    end 
        
    %EXPECTED (GROSS) SURPLUS OF INVESTOR (not incuding the cost of entry to the platform)   
             
    %a) from bialteral trade
    uB(idxd,:) =rhoj(idxd,:).*xid;
        
    %b) from platform trade (conditional expectation)
    a       = psi(idxd,:);
    Mills   = -f(a)./F(a);  
    ExpfunT = munu + sigmanu.*Mills;
        
    uE(idxd,:)  = -ExpfunT + (1-rhoj(idxd,:)).*(psi(idxd,:)-c +xid );
    uE2(idxd,:) = NaN.*uE(idxd,:); %--->NOT COMPUTED ANYMORE

    Ecost(idxd,:) =  (1-rhoj(idxd,:)).*c;

    
    %EXPECTED YIELD   
    if F(a)~=1     %some trade bilateral
        YB(idxd,:) = rhoj(idxd,:).*(theta(idxd)-xijH(idxd) + xid) + (munu + sigmanu.*(f(a)./(1-F(a))));
	elseif F(a)==1 %all trade on the platform
        YB(idxd,:)=0;
    end    
	for j=1:Jt
        idxdother = setdiff(1:Imax, idxd(j));
        YE(idxd(j),:) = (1-rhoj(idxd(j),:)).* (s0jH(idxd(j))*qfull(idxd(j)) + s0kH(idxd(j), idxdother)*qfull(idxdother));    
	end
         

    %EXPECTED PROFIT FOR THE DEALER
    %Profit from bilateral trades 
	piB(idxd,:) = (vdj(idxd,:) -theta(idxd) +xijH(idxd) - xid).*(1-F(a)) ...
                    - (munu + sigmanu*(f(a)./(1-F(a))));
    piB(F(a)==1,:)=0;           
                
                
	%Profit from platform trades
	for j=1:Jt
        idxdother=setdiff(1:Imax, idxd(j));
        piE(idxd(j),:) = sjH(idxd(j),:).*(vdj(idxd(j),:)-qfull(idxd(j))+ sum(sjNH(idxd(j),idxdother)'.*(vdj(idxdother,:)-qfull(idxdother)))) ;
    end 
  
    %WELFARE 
    %A) EXCLUDING THE HOMEDEALER BENEFIT
    for j=1:Jt
        idxdother=setdiff(1:Imax, idxd(j));
                
        W(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(vdj(idxd(j),:)+xijB(idxd(j))-(theta(idxd(j))+munu))...
                  +sum(sjNH(idxd(j),idxdother)).*(vdj(idxd(j),:)+xijB(idxd(j))-(theta(idxd(j))+munu));
                                  
        W2(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(vdj2(idxd(j),:)+xijB(idxd(j))-(theta(idxd(j))+munu))...
                     +sum(sjNH(idxd(j),idxdother)).*(vdj2(idxd(j),:)+xijB(idxd(j))-(theta(idxd(j))+munu));
               
        Wxij(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(xijB(idxd(j)))...
                    +sum(sjNH(idxd(j),idxdother)).*(xijB(idxd(j))); 
                 
        WxijH(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(xijB(idxd(j)) -xijB(idxd(j)));           

        Wvdj(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(vdj(idxd(j),:))...
                     +sum(sjNH(idxd(j),idxdother)).*(vdj(idxd(j),:)); 

                        
    end 
    %B) INCLUDIGN THE HOMEDEALER BEnEFIT
	for j=1:Jt
       idxdother=setdiff(1:Imax, idxd(j));
                
        WwH(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(vdj(idxd(j),:)+xijH(idxd(j))-(theta(idxd(j))+munu))...
                    +sum(sjNH(idxd(j),idxdother)).*(vdj(idxd(j),:)+xijB(idxd(j))-(theta(idxd(j))+munu));
                                 
        W2wH(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(vdj2(idxd(j),:)+xijH(idxd(j))-(theta(idxd(j))+munu))...
                     +sum(sjNH(idxd(j),idxdother)).*(vdj2(idxd(j),:)+xijB(idxd(j))-(theta(idxd(j))+munu));
              
        WxijwH(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(xijB(idxd(j)))...
                    +sum(sjNH(idxd(j),idxdother)).*(xijB(idxd(j))); 
                
        WxijHwH(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(xijH(idxd(j)) -xijB(idxd(j)));           
    
        WvdjwH(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(vdj(idxd(j),:))...
                     +sum(sjNH(idxd(j),idxdother)).*(vdj(idxd(j),:));                     
    end 
    
	W=sum(W);
    W2=sum(W2);
	Wxij=sum(Wxij);
    WxijH=sum(WxijH);
	Wvdj=sum(Wvdj);
    
    WwH=sum(WwH);
    W2wH=sum(W2wH);
	WxijwH=sum(WxijwH);
    WxijHwH=sum(WxijHwH);
	WvdjwH=sum(WvdjwH);
    
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%BUYERS  WITHOUT ACCESS 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   

elseif access==0 && side==1 
    
	%use the estimated value of the dealer
	vdj(idxd,:)     =vdjSQ(:,idxd)';
    
    %Shrinking dealer's value towards theta
    distance = vdj-theta;
    vdj2     = theta+(1-0.16)*distance;
      
	%Probability to trade OTC
	rhoj(idxd,:) = ones(size(idxd,2),1); 
	sj(idxd,:)   = zeros(size(idxd,2),1); 
	s0j(idxd,:)  = zeros(size(idxd,2),1);  
        
	%EXPECTED PROFIT OF INVESTOR    
        
	uB(idxd,:) =xid; 
	uE(idxd,:) =0.*xid; 
	Ecost(idxd,:)=0.*xid; 
	YE(idxd,:) =0.*xid; 
	YB(idxd,:) = theta(idxd) + munu -xijH(idxd) + xid;

    %EXPECTED PROFIT FOR THE DEALER
        
    %Profit from bilateral trades 
    piB(idxd,:) = vdj(idxd,:) -theta(idxd) -munu +xijH(idxd)  - xid;
    %Profit from platform trades
    piE(idxd,:) = 0.*vdj(idxd,:);
        
    %OF set to NaN (never maximized over!)
    OF=NaN;
        
    %Welfare
    %A) INCLUDING THE HOME DEALER BENEFIT
	WwH=sum((rhoj(idxd,:)).*(vdj(idxd,:)+xijH(idxd) -(theta(idxd)+munu)));
    W2wH=sum((rhoj(idxd,:)).*(vdj2(idxd,:)+xijH(idxd) -(theta(idxd)+munu)));
	WxijwH=sum(rhoj(idxd,:).*(xijB(idxd)));
    WxijHwH=sum((rhoj(idxd,:)+sj(idxd,:)).*(xijH(idxd)-xijB(idxd)));
    WvdjwH=sum(rhoj(idxd,:).*(vdj(idxd,:)));
    
    %B) EXCLUDING THE HOMEDEALER BENEFIT
    W=sum((rhoj(idxd,:)).*(vdj(idxd,:)+xijB(idxd) -(theta(idxd)+munu)));
    W2=sum((rhoj(idxd,:)).*(vdj2(idxd,:)+xijB(idxd) -(theta(idxd)+munu)));
	Wxij=sum(rhoj(idxd,:).*(xijB(idxd)));
    WxijH=sum((rhoj(idxd,:)+sj(idxd,:)).*(xijB(idxd)-xijB(idxd)));
	Wvdj=sum(rhoj(idxd,:).*(vdj(idxd,:)));
     
   
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%SELLERS  WITH ACCESS 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      

%Note: Only run the status quo for sellers! Not the counterfactuals 

elseif access==1 && side==-1
    
    %PSI CUTOFF
    for j=1:Jt
        if xij_for_investor==1
            xidj=0;
        else 
            xidj=xid(j);
        end
        psi(idxd(j),:)     =  -sigma*log(sum(exp(1/sigma*(xij(idxd,idxd(j))-qfull(idxd)))))+xidj -c- theta(idxd(j));
        psi_opt(idxd(j),:) =  -max(xij(idxd,idxd(j))-qfull(idxd)) -c- theta(idxd(j));
    end
             
    
    %MARKET SHARES
    %(a) OTC makret
    rhoj(idxd,:)     = F(psi(idxd,:));
    rhoj_opt(idxd,:) = F(psi_opt(idxd,:));
   
	%(b) platform
    s0jNH=NaN*ones(Imax,Imax);
    s0kH =NaN*ones(Imax,Imax);
    sjNH =NaN*ones(Imax,Imax);
    s0jNHprime =NaN*ones(Imax,Imax);
    
    h               = NaN*ones(Imax,1);
    s0jH            = NaN*ones(Imax,1);
    s0jHprime        = NaN*ones(Imax,1);
    sjprime         = NaN*ones(Imax,1);
    etaEj           = NaN*ones(Imax,1);
    etaDj           = NaN*ones(Imax,1);  
  
            
    for j=1:Jt
        idxdother       = setdiff(1:Imax, idxd(j));

        %conditional platform market share
        s0jH(idxd(j),:)  = exp(1/sigma*(xij(idxd(j),idxd(j)) - qfull(idxd(j))))/(sum(exp(1/sigma*(xij(idxd,idxd(j))-qfull(idxd)))));
            
        for k=1:Jt-1
            s0jNH(idxd(j),idxdother(k)) = exp(1/sigma*(xijB(idxd(j)) - qfull(idxd(j))))/(sum(exp(1/sigma*(xij(idxd,idxdother(k))-qfull(idxd))))); %investors with another homedealer who buy from j
            s0kH(idxd(j),idxdother(k))  = exp(1/sigma*(xijB(idxdother(k)) - qfull(idxdother(k))))/(sum(exp(1/sigma*(xij(idxd,idxd(j))-qfull(idxd))))); %investors with home dealer j who buy from dealer k
        end
            
        %conditional platform market share, unconditional on who is the homedealer 
        h(idxd) = (1-rhoj(idxd,:))./ nansum(1-rhoj(idxd,:));
        s0j(idxd(j),:)= h(idxd(j)).*s0jH(idxd(j),:) + sum(h(idxdother)'.*s0jNH(idxd(j),idxdother));
              
        %unconditional platform market share
        sjH(idxd(j),:)          = s0jH(idxd(j),:)*(1-F(psi(idxd(j)))); %of home investors
        sjNH(idxd(j),idxdother) = (s0jNH(idxd(j),idxdother)'.*(1-F(psi(idxdother))))'; 
        sj(idxd(j),:)           = sjH(idxd(j),:) + sum(sjNH(idxd(j),idxdother)); %total          
    end
    
     for j=1:Jt
        
        s0jHprime(idxd(j),:)    = -1/sigma * s0jH(idxd(j),:) * nansum(s0jNH( :, idxd(j)));
        
        idxdother = setdiff(1:Imax, idxd(j));
        for k=1:length(idxdother)
            idxotherkj=setdiff(idxdother, idxdother(k));
            s0jNHprime(idxd(j),idxdother(k))   =  -1/sigma *s0jNH(idxd(j), idxdother(k))*(s0jH(idxdother(k),:)+sum(s0jNH(idxdother(k), idxotherkj)));
        end
     end 
    
    for j=1:Jt
        idxdother = setdiff(1:Imax, idxd(j));       
        sjprime(idxd(j),:) = s0jHprime(idxd(j),:).*(1-F(psi(idxd(j)))) + sum(s0jNHprime(idxd(j),idxdother)'.* (1-F(psi(idxdother)))) ...
                                 - s0jH(idxd(j),:).^2*f(psi(idxd(j)))  - sum(s0jNH(idxd(j),idxdother).^2'.* f(psi(idxdother)));    
                                  
    end       
    etaEj(idxd,:) = sjprime(idxd)./sj(idxd) .*qfull(idxd);
    etaDj(idxd,:) = f(psi(idxd)).*s0jH(idxd)/F(psi(idxd))*qfull(idxd);    
    
    %VALUE OF DEALER
    if homedealer==0 && xij_for_investor==1     %benchmark in benchark model
        xijnow = xij;
    elseif homedealer==0 && xij_for_investor==0  
    	xijnow = 0;
    elseif homedealer==1 && xij_for_investor==0 %benchmark with homedealer model
    	xijnow = xijB(idxd);
    elseif homedealer==1 && xij_for_investor==1 
        xijnow = xijH(idxd);
    end 
        
    if estimate==1
        %back out value
        a0(idxd,:)      = etaDj(idxd,:)./etaEj(idxd,:) .*rhoj(idxd,:)./sj(idxd,:);      
        vdj(idxd,:) 	= (qfull(idxd) .*(1+1./etaEj(idxd,:))+ (psi(idxd,:) +theta(idxd)+ xijnow ).*a0(idxd,:))./(1+a0(idxd,:));
            
    elseif estimate==0
        %use the estimated value of the dealer    
        vdj(idxd,:)     =vdjSQ(:,idxd)';
    end 

    %Shrinking dealer's value towards theta
    distance = -vdj+theta;
    vdj2     = theta-(1-0.16)*distance;
  

    %OBJECTIVE FUNCTION FOR QUOTES
    piDprimej(idxd,:) = (-vdj(idxd,:) + psi(idxd,:) +theta(idxd) +xijnow ) .* etaDj(idxd,:) .* rhoj(idxd,:)./qfull(idxd);
    OF0 = sum((vdj(idxd,:) - qfull(idxd).*(1+1./etaEj(idxd,:).*(1+piDprimej(idxd,:)./sj(idxd,:)))).^2);
    if isnan(OF0)==0 
        OF = sum((vdj(idxd,:) - qfull(idxd).*(1+1./etaEj(idxd,:).*(1+piDprimej(idxd,:)./sj(idxd,:)))).^2);
    else 
        OF = 10^100;
    end 
        

    %EXPECTED (GROSS) SURPLUS OF INVESTOR (not incuding the cost of entry to the platform)   
             
    %a) from bialteral trade
    uB(idxd,:) =rhoj(idxd,:).*xid;
        
    %b) from platform trade (conditional expectation)
    a       = psi(idxd,:);
    Mills   = f(a)./(1-F(a));  
    ExpfunT = munu + sigmanu.*Mills ;
        
    uE(idxd,:)  = ExpfunT + (1-rhoj(idxd,:)).*(-psi(idxd,:)+c -xid );
    uE2(idxd,:) = NaN.*uE(idxd,:); 

    Ecost(idxd,:) =  (1-rhoj(idxd,:)).*c;

    
    %EXPECTED YIELD   
    if F(a)~=1     %some trade bilateral
        YB(idxd,:) = rhoj(idxd,:).*(theta(idxd)+xijH(idxd) - xid) + (munu - sigmanu.*(f(a)./F(a)));
	elseif F(a)==1 %all trade on the platform
        YB(idxd,:)=0;
    end    
	for j=1:Jt
        idxdother = setdiff(1:Imax, idxd(j));
        YE(idxd(j),:) = (1-rhoj(idxd(j),:)).* (s0jH(idxd(j))*qfull(idxd(j)) + s0kH(idxd(j), idxdother)*qfull(idxdother));  
    end
    
  

    %EXPECTED PROFIT FOR THE DEALER
    %Profit from bilateral trades 
	piB(idxd,:) = (theta(idxd) +xijH(idxd) - xid -vdj(idxd,:)).*F(a) ...
                    + (munu - sigmanu*(f(a)./(F(a))));
    piB(F(a)==0,:)=0;             
                
	%Profit from platform trades
	for j=1:Jt
        idxdother=setdiff(1:Imax, idxd(j));
        piE(idxd(j),:) = sjH(idxd(j),:).*(-vdj(idxd(j),:)+qfull(idxd(j))+ sum(sjNH(idxd(j),idxdother)'.*(-vdj(idxdother,:)+qfull(idxdother)))) ;
    end 
    

    %WELFARE
    %A) INCLUDING THE HOME DEALER BENEFIT
	for j=1:Jt
        idxdother=setdiff(1:Imax, idxd(j));
              
        WwH(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(-vdj(idxd(j),:)+xijH(idxd(j))+(theta(idxd(j))+munu))...
                     +sum(sjNH(idxd(j),idxdother)).*(-vdj(idxd(j),:)+xijB(idxd(j))+(theta(idxd(j))+munu));
       
        W2wH(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(-vdj2(idxd(j),:)+xijH(idxd(j))+(theta(idxd(j))+munu))...
                     +sum(sjNH(idxd(j),idxdother)).*(-vdj2(idxd(j),:)+xijB(idxd(j))+(theta(idxd(j))+munu));
     
        WxijwH(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(xijB(idxd(j)))...
                     +sum(sjNH(idxd(j),idxdother)).*(xijB(idxd(j)));  
                
        WxijHwH(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(xijH(idxd(j)) -xijB(idxd(j)));           
    
    
        WvdjwH(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(-vdj(idxd(j),:))...
                   +sum(sjNH(idxd(j),idxdother)).*(-vdj(idxd(j),:)); 
      
    end 
    
    %B) EXCLUDING THE HOMEDEALER BENEFIT
    for j=1:Jt
        idxdother=setdiff(1:Imax, idxd(j));
               
        W(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(-vdj(idxd(j),:)+xijB(idxd(j))+(theta(idxd(j))+munu))...
                   +sum(sjNH(idxd(j),idxdother)).*(-vdj(idxd(j),:)+xijB(idxd(j))+(theta(idxd(j))+munu));
        
        W2(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(-vdj2(idxd(j),:)+xijB(idxd(j))+(theta(idxd(j))+munu))...
                     +sum(sjNH(idxd(j),idxdother)).*(-vdj2(idxd(j),:)+xijB(idxd(j))+(theta(idxd(j))+munu));
      
        Wxij(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(xijB(idxd(j)))...
                     +sum(sjNH(idxd(j),idxdother)).*(xijB(idxd(j)));  
                 
        WxijH(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(xijH(idxd(j)) -xijB(idxd(j)));           


        Wvdj(j)=(rhoj(idxd(j),:)+sjH(idxd(j),:)).*(-vdj(idxd(j),:))...
                     +sum(sjNH(idxd(j),idxdother)).*(-vdj(idxd(j),:)); 
      
    end 
    
	W=sum(W);
    W2=sum(W2);
	Wxij=sum(Wxij);
    WxijH=sum(WxijH);
	Wvdj=sum(Wvdj);
    
    WwH=sum(WwH);
    W2wH=sum(W2wH);
	WxijwH=sum(WxijwH);
    WxijHwH=sum(WxijHwH);
	WvdjwH=sum(WvdjwH);
    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%SELLERS  WITHOUT ACCESS 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      
    
elseif side==-1 && access==0  
    
    %use the estimated value of the dealer
    vdj(idxd,:) =vdjSQ(:,idxd)';
    
    %Shrinking dealer's value towards theta
    distance = -vdj+theta;
    vdj2     = theta-(1-0.16)*distance;
  
    %Probability to trade OTC
    rhoj(idxd,:)= ones(size(idxd,2),1);  
	sj(idxd,:)  = zeros(size(idxd,2),1);  
	s0j(idxd,:) = zeros(size(idxd,2),1);  
        
	uB(idxd,:) =xid; 
	uE(idxd,:) =0.*xid; 
	Ecost(idxd,:) =0.*xid; 
	YE(idxd,:) =0.*xid; 
	YB(idxd,:) = theta(idxd) + munu + xijH(idxd) -xid;
    
	%Profit from bilateral trades 
	piB(idxd,:) = theta(idxd) + munu +xijH(idxd) - xid - vdj(idxd,:);
	%Profit from platform trades
	piE(idxd,:) = 0.*vdj(idxd,:);
                   
	%OF set to NaN (never maximized over!)
	OF=NaN;

	%Welfare
    %A) INCLUDIGN THE HOMEDEALER BENEFIT
	WwH=sum((rhoj(idxd,:)+sj(idxd,:)).*((theta(idxd)+munu)-vdj(idxd,:)+xijH(idxd)));
    W2wH=sum((rhoj(idxd,:)+sj(idxd,:)).*((theta(idxd)+munu)-vdj2(idxd,:)+xijH(idxd)));
    WxijwH=sum((rhoj(idxd,:)+sj(idxd,:)).*(xijB(idxd)));
    WxijHwH=sum((rhoj(idxd,:)+sj(idxd,:)).*(xijH(idxd)-xijB(idxd)));
    WvdjwH=sum((rhoj(idxd,:)+sj(idxd,:)).*(-vdj(idxd,:)));
    
    %B) EXCLUDING THE HOMEDEALER BENEFIT
    W=sum((rhoj(idxd,:)+sj(idxd,:)).*((theta(idxd)+munu)-vdj(idxd,:)+xijB(idxd)));
    W2=sum((rhoj(idxd,:)+sj(idxd,:)).*((theta(idxd)+munu)-vdj2(idxd,:)+xijB(idxd)));
    Wxij=sum((rhoj(idxd,:)+sj(idxd,:)).*(xijB(idxd)));
    WxijH=sum((rhoj(idxd,:)+sj(idxd,:)).*(xijB(idxd)-xijB(idxd)));
    Wvdj=sum((rhoj(idxd,:)+sj(idxd,:)).*(-vdj(idxd,:)));
end
